#include "BCoords.h"

double Determinant(Point i_rowa, Point i_rowb, Point i_rowc)
	{
	return
		i_rowa.m_x * (i_rowb.m_y * i_rowc.m_z - i_rowb.m_z * i_rowc.m_y) -
		i_rowb.m_x * (i_rowa.m_y * i_rowc.m_z - i_rowa.m_z * i_rowc.m_y) +
		i_rowc.m_x * (i_rowa.m_y * i_rowb.m_z - i_rowa.m_z * i_rowb.m_y);
	}

bool InsideTriangle(Triangle i_triangle, Point i_point)
	{
	auto det = Determinant(i_triangle.m_a, i_triangle.m_b, i_triangle.m_c);
	auto det_x = Determinant(
		Point{ i_point.m_x, i_triangle.m_b.m_x, i_triangle.m_c.m_x },
		Point{ i_point.m_y, i_triangle.m_b.m_y, i_triangle.m_c.m_y },
		Point{ i_point.m_z, i_triangle.m_b.m_z, i_triangle.m_c.m_z });
	auto det_y = Determinant(
		Point{ i_triangle.m_a.m_x, i_point.m_x, i_triangle.m_c.m_x },
		Point{ i_triangle.m_a.m_y, i_point.m_y, i_triangle.m_c.m_y },
		Point{ i_triangle.m_a.m_z, i_point.m_z, i_triangle.m_c.m_z });
	auto det_z = Determinant(
		Point{ i_triangle.m_a.m_x, i_triangle.m_b.m_x, i_point.m_x },
		Point{ i_triangle.m_a.m_y, i_triangle.m_b.m_y, i_point.m_y },
		Point{ i_triangle.m_a.m_z, i_triangle.m_b.m_z, i_point.m_z });

	auto lambda_x = det_x / det;
	auto lambda_y = det_y / det;
	auto lambda_z = det_z / det;

	return (
		lambda_x >= 0 &&
		lambda_y >= 0 &&
		lambda_z >= 0 &&
		det_x + det_y + det_z == det);

	}